//
//  ZXComposePhotosView.m
//  ZXOCWeiBo
//
//  Created by mac on 15/12/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXComposePhotosView.h"

#define kImgCountPerRow 4

@interface ZXComposePhotosView()

@property(nonatomic,strong)UIImageView* imageView;

@end

@implementation ZXComposePhotosView

-(void)addImag:(UIImage *)img
{
    //显示图片
    UIImageView * imgView = [[UIImageView alloc]init];
    imgView.image = img;
    [self addSubview:imgView];
    
    NSInteger count = self.subviews.count;
    //间距
    CGFloat margin = 5;
    CGFloat imgW = (self.w - (kImgCountPerRow + 1) * margin) / kImgCountPerRow;
    CGFloat imgH = imgW;
    
    for (NSInteger i = 0; i < count ; i++)
    {
        CGFloat cols = i % kImgCountPerRow;
        CGFloat row = i / kImgCountPerRow;
        
        CGFloat imgX = margin + (imgW + margin) * cols;
        CGFloat imgY = margin + (imgH + margin) * row;
        UIView * subView = self.subviews[i];
        subView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    }
    
//    遍历所有UIImageView 然后设置fram
    ZXLog(@"添加图片 %ld",self.subviews.count);
}

-(BOOL)harImgs
{
    return self.subviews.count > 0;
}

-(NSArray *)images
{
    NSMutableArray * imgsM = [NSMutableArray array];
    for (UIImageView * imgView in self.subviews)
    {
        [imgsM addObject:imgView.image];
    }
    return [imgsM copy];
}

//-(instancetype)initWithFrame:(CGRect)frame
//{
//    if (self = [super initWithFrame:frame])
//    {
//        for (int i = 0; i < 9; i++)
//        {
//            UIImageView * imageView  = [[UIImageView alloc]init];
////            self.imageView = imageView;
//            
//            [self addSubview:imageView];
//        }
//       
//    }
//    return self;
//}
//
//-(void)layoutSubviews
//{
//    [super layoutSubviews];
//    
////    self.imageView.frame = CGRectMake(20, 20, 80, 80);
////    self.imageView.image = self.image;
//    
//    for (UIImageView * imageView in self.subviews)
//    {
//        imageView.frame = CGRectMake(0,20, 80, 80);
//        imageView.image = self.image;
//    }
//}

@end
